<template>
  <div>
    <el-form ref="form" label-width="80px">
      <el-form-item label="SPU名称">{{ spu.spuName }}</el-form-item>
      <el-form-item label="SKU名称">
        <el-input placeholder="SKU名称" v-model="skuInfo.skuName"></el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input
          placeholder="价格(元)"
          type="number"
          v-model.number="skuInfo.price"
        ></el-input>
      </el-form-item>
      <el-form-item label="重量(千克)">
        <el-input placeholder="重量(千克)" v-model="skuInfo.weight"></el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input
          type="textarea"
          placeholder="规格描述"
          rows="4"
          v-model="skuInfo.skuDesc"
        ></el-input>
      </el-form-item>
      <el-form-item label="平台属性">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item
            :label="attr.attrName"
            v-for="attr in attrInfoList"
            :key="attr.id"
          >
            <el-select v-model="attr.attrIdAndValueId" placeholder="请选择">
              <el-option
                v-for="attrValue in attr.attrValueList"
                :key="attrValue.id"
                :label="attrValue.valueName"
                :value="`${attr.id}:${attrValue.id}`"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item
            v-for="saleAttr in spuSaleAttrList"
            :key="saleAttr.id"
            :label="saleAttr.saleAttrName"
          >
            <el-select v-model="saleAttr.attrIdAndValueId" placeholder="请选择">
              <el-option
                v-for="saleAttrValue in saleAttr.spuSaleAttrValueList"
                :key="saleAttrValue.id"
                :label="saleAttrValue.saleAttrValueName"
                :value="`${saleAttr.id}:${saleAttrValue.id}`"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片列表">
        <el-table
          ref="multipleTable"
          tooltip-effect="dark"
          style="width: 100%"
          border
          :data="spuImageList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column
            prop="imgUrl"
            label="图片"
            width="width"
            align="center"
          >
            <template slot-scope="{ row }">
              <img :src="row.imgUrl" width="100px" />
            </template>
          </el-table-column>
          <el-table-column prop="imgName" label="名称" width="width">
          </el-table-column>
          <el-table-column label="操作" width="width">
            <template slot-scope="{ row }">
              <el-button
                type="primary"
                size="mini"
                v-if="row.isDefault == 0"
                @click="changeDefault(row)"
                >设为默认</el-button
              >
              <el-tag type="success" v-else>默认</el-tag>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addSku">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "SkuForm",
  data() {
    return {
      // 储存图片的信息
      spuImageList: [],
      // 存储销售属性
      spuSaleAttrList: [],
      // 存储平台属性的数据
      attrInfoList: [],
      // 收集sku数据的字段
      skuInfo: {
        // 父组件给予的数据
        category3Id: 0,
        spuId: 0,
        tmId: 0,
        // 需要通过双向绑定v-model收集
        price: "",
        skuDesc: "",
        skuName: "",
        weight: "",
        // 平台属性
        skuAttrValueList: [],
        // 默认图片
        skuDefaultImg: "",
        // 收集图片字段
        skuImageList: [],
        // 销售属性
        skuSaleAttrValueList: [],
      },
      spu: {},
      imgList: [],
    };
  },
  methods: {
    async getData(spu, category1Id, category2Id, category3Id) {
      // 收集父组件传来的数据
      this.skuInfo.category3Id = category3Id;
      this.skuInfo.spuId = spu.id;
      this.skuInfo.tmId = spu.tmId;
      this.spu = spu;
      // 获取spu图片数据
      let spuImageResult = await this.$API.spu.reqSpuImageList(spu.id);
      if (spuImageResult.code == 200) {
        let list = spuImageResult.data;
        list.forEach((item) => {
          item.isDefault = 0;
        });
        this.spuImageList = list;
      }
      // 销售属性的数据
      let saleResult = await this.$API.spu.reqSpuSaleAttrList(spu.id);
      if (saleResult.code == 200) {
        this.spuSaleAttrList = saleResult.data;
      }
      // 平台属性的数据
      let attrResult = await this.$API.spu.reqAttrInfoList(
        category1Id,
        category2Id,
        category3Id
      );
      if (attrResult.code == 200) {
        this.attrInfoList = attrResult.data;
      }
    },
    // table表格复选框按钮的事件
    handleSelectionChange(params) {
      this.imgList = params;
    },
    // 排他操作
    changeDefault(row) {
      // 将图片列表的所有数据变为0
      this.spuImageList.forEach((item) => {
        item.isDefault = 0;
      });
      // 将点击的那一张设为默认
      row.isDefault = 1;
      // 收集默认图片地址
      this.skuInfo.skuDefaultImg = row.imgUrl;
    },
    // 保存按钮
    async addSku() {
      const { attrInfoList, skuInfo, spuSaleAttrList, imgList } = this;
      // 整理平台属性数据
      skuInfo.skuAttrValueList = attrInfoList.reduce((prev, item) => {
        if (item.attrIdAndValueId) {
          const [attrId, valueId] = item.attrIdAndValueId.split(":");
          prev.push({ attrId, valueId });
        }
        return prev;
      }, []);
      // 整理销售属性数据
      skuInfo.skuSaleAttrValueList = spuSaleAttrList.reduce((prev, item) => {
        if (item.attrIdAndValueId) {
          const [saleAttrId, saleAttrValueId] =
            item.attrIdAndValueId.split(":");
          prev.push({ saleAttrId, saleAttrValueId });
        }
        return prev;
      }, []);
      // 整理图片数据
      skuInfo.skuImageList = imgList.map((item) => {
        return {
          imgUrl: item.imgUrl,
          imgName: item.imgName,
        };
      });
      let result = await this.$API.spu.reqAddSku(skuInfo);
      console.log(result);
      if (result.code) {
        
        this.$message({ type: "success", message: "添加SKU成功" });
        this.$emit("cancel");
        Object.assign(this._data, this.$options.data());
      }
    },
    // 取消按钮
    cancel() {
      this.$emit("cancel");
      // 清除数据
      Object.assign(this._data, this.$options.data());
    },
  },
};
</script>

<style lang="scss" scoped></style>
